package com.jnu.book;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.jnu.book.data.CircleSpriter;

import java.util.ArrayList;

public class GameView extends SurfaceView implements SurfaceHolder.Callback{
    private SurfaceHolder surfaceHolder;
    private boolean isStop=false;
    DrawThread drawThread;
    private float touchX=-1,touchY=-1;
    private boolean isTouched=false;
    private boolean state=true;
    public GameView(Context context) {
        super(context);
        initView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        drawThread = new DrawThread();
        drawThread.start();
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(MotionEvent.ACTION_DOWN==event.getAction()){
                    touchX=event.getRawX();
                    touchY=event.getRawY();
                    isTouched = true;
                }
                return false;
            }
        });
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        drawThread.myStop();
    }


    private class DrawThread extends Thread{
        private ArrayList<CircleSpriter> spriters=new ArrayList<>();
        public DrawThread() {
            spriters.add(new CircleSpriter(175,275,100,GameView.this.getWidth(),GameView.this.getHeight()));
            //spriters.add(new CircleSpriter(300,300,100,GameView.this.getWidth(),GameView.this.getHeight()));
        }

        private void myStop(){
            isStop=true;
        }
        @Override
        public void run() {
            super.run();
            Canvas canvas = null;
            int hitCount=0;
            while (!isStop){
                try{
                    Paint paint=new Paint();
                    paint.setColor(Color.BLACK);
                    paint.setStyle(Paint.Style.STROKE);
                    canvas=surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.GRAY);
                    for(int i=0;i<4;i++){     //绘制格子
                        for(int j=0;j<4;j++){
                            canvas.drawRect(50+200*j,150+200*i,250+200*j,350+200*i,paint);
                        }
                    }

                    if(isTouched){        //判断是否点击
                        for(int i=0;i<spriters.size();i++){
                            if(spriters.get(i).isShot(touchX,touchY))
                                hitCount++;
                        }
                    }
                    for(int i=0;i<spriters.size();i++){
                        spriters.get(i).move();
                    }
                    if(state){                  //根据状态变量判断地鼠是否绘制
                        for(int i=0;i<spriters.size();i++){
                            spriters.get(i).draw(canvas);
                        }
                        state=false;
                    }
                    else{               //这次状态是消失，下一次会出现
                        state=true;
                    }
                    isTouched=false;
                    paint.setStyle(Paint.Style.FILL);
                    paint.setTextSize(100);
                    paint.setColor(Color.GREEN);
                    canvas.drawText("hit"+hitCount,50,100,paint);
                }catch (Exception e){

                }finally {
                    if(canvas!=null){
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
